define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    var fileItem = require("./items/fileItem.html");
    var fileBlockTpl = require("./blocks/fileBlockTpl.html")




    var composeFileView = require('./composes/composeFileView.html');
    var fileView = require("./preview/fileView.html");
    var filefooter = require("./footers/filefooter.html")


    var render = template.compile(fileBlockTpl);
    var render_item = template.compile(fileItem);

    var emptyHtml = require('./emptys/file.html')
    var Ffiles = function(element, layout) {
        this.element = $(element);
        this.layout = layout;
        this.selector = ".js_file_container .file_unit:last";
        this.type = window.message_config.files_type_ids[5];
        this.init();
    }
    Ffiles.prototype.init = function() {
        this.element.empty();
        this.events();
        this.selectView();
        this.fileModel = {
            FileID: 0,
            File: {},
            size: ''
        }
        this.model = {}
    };
    Ffiles.prototype.templateData = function() {
        return {
            model: this.model,
            File: this.fileModel.File,
            app: window.message_config.app,
            size: this.fileModel.size
        }
    };
    Ffiles.prototype.validation = function() {
        return true;
    };
    Ffiles.prototype.selectData = function(data) {
        var _self = this;
        $(".js_add_message").removeAttr('disabled').bind('click', function(event) {
            _self.generate(data)
        });
    }
    Ffiles.prototype.sync = function(data) {
        var _self = this;
        var preview = template.compile(fileView)(this.templateData());
        this.layout.showPreview();
        this.layout.setPreview(preview);
    }
    Ffiles.prototype.events = function() {
        var _self = this;
        $(document).off('click.js_file_container').on("click.js_file_container", ".file_unit", function() {
            var $this = $(this),
                $fileId = $this.closest('li').data("fileId");
            $(".js_file_container .file_unit").removeClass('selected');
            $this.addClass('selected').siblings().removeClass('selected');
            $fileId && _self.selectData(window.files[_self.type][$fileId]);

        })
    };
    Ffiles.prototype.selectView = function() {
        var $footer = $(filefooter);
        var _self = this;
        var list_data = {};
        var uploadimte = null;
        $(".js_new_message", $footer).uploader({
            multi: true,
            url: window.message_config.upload_path,
            max_count: 100,
            data: {
                type: _self.type,
                data: window.message_config.upload_params
            },
            FilesAdded: function(up, files) {
                $.each(files, function(index, file) {
                    var m = method.getFormatDateStrNow();
                    _self.monthhtml(m, false, file);
                });
            },
            UploadProgress: function(up, file) {
                var upfileid = "upload{0}".format(file.id);
                uploadimte = $("#{0}".format(upfileid));
                if (uploadimte.length) {
                    var percent = file.percent + "%";
                    $(".js_update_text", uploadimte).text(percent)
                    $(".js_process", uploadimte).width(percent)
                };

            },
            FileUploaded: function(up, file, res) {
                if ($.isPlainObject(res)) {
                    list_data[file.id] = res;
                } else {
                    uploadimte.closest('li').remove();
                    up.files.removeFile(file);
                    config.msg.info(res || config.lang.uplodError);
                }
            },
            UploadComplete: function(up, files) {
                var last_item = list_data[files[files.length - 1].id];
                last_item && _self.generate(last_item);
            }
        }, "file")
        this.layout.setFooter($footer);
    }
    Ffiles.prototype.generate = function(data) {
        var _self = this;
        if (data) {
            this.fileModel.FileID = data.file_id;
            this.fileModel.File = data.File;
            this.size = data.size;
        };
        var warp = template.compile(composeFileView)(this.templateData());
        var $warp = $(warp);


        var preview = template.compile(fileView)(this.templateData());

        this.layout.setWarp($warp);
        this.layout.showPreview();
        this.layout.setPreview(preview);
        this.layout.setMassfooter(this);

        this.layout.blcokDestroy()
    }
    Ffiles.prototype.list = function(data) {
        var _self = this;
        if (data.last_file_item.length) {
            this.element.append(render({
                title: "最近",
                file_items: render_item({
                    list: data.last_file_item
                })
            }));
        }
        if (data.file_item.length) {
            $.each(data.file_item, function(index, item) {
                var m = method.getFormatDateStr(item.update_time);
                _self.monthhtml(m, item);
            });
        } else {
            this.element.html(emptyHtml)
        }
    };
    Ffiles.prototype.monthhtml = function(m, item, file) {
        var md = $('.js_file_container .card_collection_title[title="' + m + '"]');

        var rhtml = item ? render_item({
            list: [item]
        }) : render_item({
            ID: file.id,
            File: {
                name: file.name,
                format: method.getfileExt(file.name).toString()
            }
        });
        if (md.length > 0) {
            md.next("ul").addhtml(rhtml, item);
        } else {
            $(".empty_placeholder_wrap").remove()
            this.element.addhtml(render({
                title: m,
                file_items: rhtml
            }), item);
        }
    }
    return Ffiles;

})
